using System;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Rendering;

public class PlayerLength : NetworkBehaviour
{
    /// <summary>
    /// 尾巴预制体
    /// </summary>
    [SerializeField]
    private GameObject _tailPrefab;
    
    /// <summary>
    /// 长度
    /// </summary>
    public NetworkVariable<ushort> length = new NetworkVariable<ushort>(1);
    
    /// <summary>
    /// 尾巴列表
    /// </summary>
    private List<GameObject> _tails = new List<GameObject>();
    
    /// <summary>
    /// 碰撞体
    /// </summary>
    private Collider2D _collider2D;
    
    /// <summary>
    /// 长度变化回调
    /// </summary>
    public static Action<ushort> OnPlayerLengthChange;
    
    /// <summary>
    /// 网络对象创建时调用
    /// </summary>
    public override void OnNetworkSpawn()
    {
        // 初始化列表
        _tails = new List<GameObject>();
        // 碰撞体
        _collider2D = GetComponent<Collider2D>();
        
        // 创建尾巴
        for(int i = 1; i < length.Value;i++)
            CreateTail(-i);
        
        // 长度变化时回调
        length.OnValueChanged = OnLengthChange;
    }

    /// <summary>
    /// 当网络对象销毁时
    /// </summary>
    public override void OnNetworkDespawn()
    {
        // 销毁尾巴
        for (int i = _tails.Count - 1; i >= 0; i--)
        {
            Destroy(_tails[i]);
        }
        _tails.Clear();
    }

    /// <summary>
    /// 长度变化时回调
    /// </summary>
    /// <param name="previousvalue">上一次的值</param>
    /// <param name="newvalue">当前新的值</param>
    private void OnLengthChange(ushort previousvalue, ushort newvalue)
    {
        if (newvalue > 1)
        {
            // 创建尾巴
            CreateTail(-length.Value);
        
            if(IsOwner)
                OnPlayerLengthChange?.Invoke(length.Value);
            
            // 播放音效
            SoundManager.Instance.PlayEatSound();
        }
        // 删除尾巴
        else
        {
            DestroyTail();
        }
    }

    /// <summary>
    /// 增加长度
    /// </summary>
    [ContextMenu("Add Length")]
    public void AddLength()
    {
        // 长度加一
        length.Value++;
    }

    /// <summary>
    /// 创建尾巴
    /// </summary>
    private void CreateTail(int sortingOrder)
    {
        // 复制一个尾巴
        GameObject tail = Instantiate(_tailPrefab,transform.position,Quaternion.identity);
        // 设置尾巴层级，越后面的尾巴层级越低
        tail.GetComponent<SortingGroup>().sortingOrder = sortingOrder;
        // 配置尾巴
        if (tail.TryGetComponent(out PlayerTail playerTail))
        {
            // 设置蛇头
            playerTail.networkedOwner = transform;
            // 设置跟随的目标
            // 没有尾巴跟随蛇头
            if (_tails.Count == 0)
                playerTail.followTransform = transform;
            // 跟随上一个尾巴
            else
                playerTail.followTransform = _tails[_tails.Count - 1].transform;
            
            // 忽略尾巴与蛇头之间的碰撞
            Physics2D.IgnoreCollision(playerTail.gameObject.GetComponent<Collider2D>(),_collider2D);
        }

        // 加入列表
        _tails.Add(tail);
    }

    /// <summary>
    /// 删除尾巴
    /// </summary>
    public void DestroyTail()
    {
        for(int i = 0; i < _tails.Count; i++)
            Destroy(_tails[i]);
        _tails.Clear();
    }

}
